﻿$(function () {
    $(".promotion").click(function () {
        $("#promotion").show();
        $("#overview").hide();
    });
    $(".overview").click(function () {
        $("#promotion").hide();
        $("#overview").show();
    });

    $("#images-slide").PikaChoose({ carousel: true, carouselOptions: { wrap: 'circular' } });

    $.ajaxSetup({
        cache: false
    });
});

function addcomment(recId) {
    var replyWindowId = "comment-add-" + recId;
    //var html = '<div><span>昵称</span><input type="text" id="txtReplyName' + recId + '" /></div><div style=" vertical-align:top">留言<textarea id="txtReplyBody' + recId + '" rows="5" cols="70"></textarea> </div><div><input type="button" style="cursor:pointer" value="马上回复" onclick="submitReplyNote(' + recId + ');" /> </div>';
    var html = '<form><textarea id="txt_comment' + recId + '" class="required"></textarea>' + '<input type="button" onclick="SubmitComment(' + recId + ');" value="Submit" /></form>';
                   

    $("#" + replyWindowId).html("");
    $("#" + replyWindowId).append(html);

    $.each($("div[id^='comment-add-']"), function () {
        if (this.id != replyWindowId) {
            $(this).hide();
        } else {
            $(this).toggle("normal");
        }
    });

    $.each($("div[id^='comment-view-']"), function () {
        $(this).hide("normal");
    });
    $.each($("div[id^='fakecomment-add-']"), function () {
        $(this).hide("normal");
    });
}

function addfakecomment(recId) {
    var replyWindowId = "fakecomment-add-" + recId;
    var html = '<form><textarea id="txt_fake_comment' + recId + '" class="required"></textarea>' + '<input type="button" onclick="SubmitFakeComment(' + recId + ');" value="Report Fake" /></form>';


    $("#" + replyWindowId).html("");
    $("#" + replyWindowId).append(html);

    $.each($("div[id^='fakecomment-add-']"), function () {
        if (this.id != replyWindowId) {
            $(this).hide();
        } else {
            $(this).toggle("normal");
        }
    });

    $.each($("div[id^='comment-add-']"), function () {
        $(this).hide("normal");
    });
    $.each($("div[id^='comment-view-']"), function () {
        $(this).hide("normal");
    });
}

function viewcomment(recId) {
    var commentWindowId = "comment-view-" + recId;
    $("#" + commentWindowId).html("");

    $.getJSON("/promotion/GetPromotionComments/" + recId, function (data) {        
        $.each(data, function (i, item) {
            var newDate = new Date(parseInt(item.createdate.replace("/Date(", "").replace(")/", ""), 10));
           
            var html = '<div class="comment-view-item"><div class="title">' + item.userName + ' add comments:</div><div class="date">' + formatDate(newDate, "YYYY-MM-DD") + '</div><div class="body">' + item.message + '</div></div>';
            $("#" + commentWindowId).append(html);
        });
    });
    
    $.each($("div[id^='comment-view-']"), function () {
        if (this.id != commentWindowId) {
            $(this).hide();
        } else {
            $(this).toggle("normal");
        }
    });

    $.each($("div[id^='comment-add-']"), function () {        
            $(this).hide("normal");     
    });
    $.each($("div[id^='fakecomment-add-']"), function () {
        $(this).hide("normal");
    });
}

function SubmitComment(recId) {
    var commentwindowid = "comment-add-" + recId;
    var body = strip($("#txt_comment" + recId).val());    
    if ($("#txt_comment" + recId).valid()) {
        var url = "/promotion/AddPromotionComments/";
        var data = "{promotionId:'" + recId + "',comment:'" + body.replace("'", "\\'") + "'}";
        jQuery.ajax({
            type: "POST",
            contentType: "application/json",
            url: url,
            data: data,
            dataType: 'json',
            success: function (returnData) {
                if (returnData["result"] == "success") {
                    $("#" + commentwindowid).hide("normal");
                    viewcomment(recId);
                    var newCount = parseInt($("#comment-count-" + recId).text()) + 1;
                    $("#comment-count-" + recId).text(newCount);
                } else if (returnData["result"] == "NotAuthenticated") {                    
                    $.colorbox({ inline: true, href:"#loginform"});
                } else {
                    alert("Error Occurs, please retry");
                }
            }
        });
    }
}

function visitsite(siteId) {   
    var url = "/Site/VisitSite/";
    var data = "{siteId:'" + siteId + "'}";
    jQuery.ajax({
        type: "POST",
        contentType: "application/json",
        url: url,
        data: data,
        dataType: 'json',
        success: function (returnData) {
            if (returnData["result"] == "success") {
                var siteUrl = returnData["url"];
                if (siteUrl != null && siteUrl != "") {
                    window.open(siteUrl);
                }
                else {
                    jAlert('Site is not available', 'Info');
                }
                
            } else if (returnData["result"] == "NotAuthenticated") {                
                $.colorbox({ inline: true, href: "#loginform" });
            } else {
                jAlert('Error Occurs, please retry', 'Failed');
            }
        }
    });
}

function SubmitFakeComment(recId) {
    var commentwindowid = "fakecomment-add-" + recId;
    var body = strip($("#txt_fake_comment" + recId).val());
    if ($("#txt_fake_comment" + recId).valid()) {
        var url = "/promotion/AddPromotionFakeComments/";
        var data = "{promotionId:'" + recId + "',fakeComment:'" + body.replace("'", "\\'") + "'}";
        jQuery.ajax({
            type: "POST",
            contentType: "application/json",
            url: url,
            data: data,
            dataType: 'json',
            success: function (returnData) {
                if (returnData["result"] == "success") {
                    $("#" + commentwindowid).hide("normal");
                    jAlert('Fake Report Success', 'Success');
                } else if (returnData["result"] == "NotAuthenticated") {
                    $.colorbox({ inline: true, href: "#loginform" });
                } else {
                    jAlert('Error Occurs, please retry', 'Failed');
                }
            }
        });
    }
}


function formatDate(date, format) {
    if (arguments.length < 2 && !date.getTime) {
        format = date;
        date = new Date();
    }
    typeof format != 'string' && (format = 'YYYY年MM月DD日 hh时mm分ss秒');
    var week = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', '日', '一', '二', '三', '四', '五', '六'];
    return format.replace(/YYYY|YY|MM|DD|hh|mm|ss|星期|周|www|week/g, function (a) {
        switch (a) {
            case "YYYY": return date.getFullYear();
            case "YY": return (date.getFullYear() + "").slice(2);
            case "MM": return date.getMonth() + 1;
            case "DD": return date.getDate();
            case "hh": return date.getHours();
            case "mm": return date.getMinutes();
            case "ss": return date.getSeconds();
            case "星期": return "星期" + week[date.getDay() + 7];
            case "周": return "周" + week[date.getDay() + 7];
            case "week": return week[date.getDay()];
            case "www": return week[date.getDay()].slice(0, 3);
        }
    });
}

function strip(html) {
    var tmp = document.createElement("DIV");
    tmp.innerHTML = html;
    return tmp.textContent || tmp.innerText;
}


